<script setup>
import { ref, onMounted } from "vue";
import { queryAllApi, saveApi,findByIdApi,updateApi,deleteApi } from "@/api/dept";
import { ElMessage, ElMessageBox } from 'element-plus';

//定义部门列表
let deptList = ref([{
  "id": 1,
  "name": "学工部",
  "createTime": "2022-09-01T23:06:29",
  "updateTime": "2022-09-01T23:06:29"
}])

//查询方法
let serach = async () => {
  let result = await queryAllApi();
  if (result.code) {
    deptList.value = result.data;
  }
}

//钩子函数
onMounted(() => {
  serach();
})

const dialogFormVisible = ref(false);
const saveTitle = ref('');
const dept = ref({ name: '' });

//弹出对话框
const addDia = () => {

    //重置表单校验信息 resetFields()
    if(deptFormRef.value){
    deptFormRef.value.resetFields();
  }
  dialogFormVisible.value = true;
  saveTitle.value = '新增部门';
  //置空对话框
  dept.value = { name: '' };
}

//新增部门方法与修改
const save = async () => {

  //数据校验
  if (!deptFormRef) return
  await deptFormRef.value.validate(async(valid, fields) => {
    if (valid) {
      // ElMessage.success("检验通过")
      let result;
      if(dept.value.id){
        //执行修改
         result = await updateApi(dept.value);
        //关闭对话框
        dialogFormVisible.value = false;
      }else{
         result = await saveApi(dept.value);
        //关闭对话框
        dialogFormVisible.value = false;
      }
      
      if (result.code) {
        ElMessage.success(result.message)
      } else {
        ElMessage.error(result.message)
      }
      //重新查询
      serach();
    } else {
      ElMessage.error('数据校验不通过')
    }
  })
}


//定义表单校验规则
const deptFormRef = ref();
const rules = ref({
  name: [
    { required: true, message: '部门名称为必填项', trigger: 'blur' },
    { min: 2, max: 10, message: '部门名称必须得在2-10位之间', trigger: 'blur' }
  ]
})

//根据id查询部门
const edit =async (id)=>{
  dialogFormVisible.value = true;
  const result =await findByIdApi(id);
  dept.value = result.data;
  saveTitle.value = '修改部门';
  //重置表单校验信息 resetFields()
  if(deptFormRef.value){
    deptFormRef.value.resetFields();
  }

}

//删除员工
const deleteDept = (id)=>{
  ElMessageBox.confirm(
    '您确定要删除部门吗?',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async() => {
      const result = await deleteApi(id);
      serach();
      if(result.code){
        ElMessage.success(result.message);
      }else{
        ElMessage.error(result.message);
      }
    })
    .catch((err) => {
      console.log(err)
     ElMessage.info("操作取消")
    })


}
</script>

<template>
  <h1>部门管理</h1>

  <div id="header"><el-button type="primary" plain @click="addDia">+新增部门</el-button></div>

  <el-table :data="deptList" border style="width: 100%">
    <el-table-column type="index" label="序号" width="100px" align="center" />
    <el-table-column prop="name" label="部门名称" width="300px" align="center" />
    <el-table-column prop="createTime" label="最后操作时间" width="300px" align="center" />
    <el-table-column prop="" label="操作" align="center">
      <template #default="scope">
        <el-button type="primary" round @click="edit(scope.row.id)">编辑</el-button>
        <el-button type="danger" round @click="deleteDept(scope.row.id)">删除</el-button>
      </template>
      
    </el-table-column>
  </el-table>

  <!-- 新增/修改部门的表单 -->
  <el-dialog v-model="dialogFormVisible" :title="saveTitle" width="500">
    <el-form :model="dept" :rules="rules" ref="deptFormRef">
      <el-form-item label="部门名称" label-width="150px" prop="name">
    
        <el-input v-model="dept.name" placeholder="请输入部门名称, 长度2-10位" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </div>
    </template>
  </el-dialog>

</template>

<style scoped>
#header {
  margin: 30px 0px;
}
</style>
